<?php
namespace app\common\model;

use think\Model;

class SinceModel extends Model {
    // 完整数据表名
    protected $table              = 'since';
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    // 时间输出格式
    protected $type = [
        'since_create_time' => 'timestamp',
        'since_update_time' => 'timestamp',
    ];

    //商户自提点列表
    public static function since_sel($where, $page_number){
        $since = SinceModel::where($where)->order('since_create_time desc')->paginate($page_number);
        if(!$since){
            return false;
        }
        return $since;
    }

    //添加自提点数据
    public static function since_add($data){
        $since = SinceModel::create($data);
        if(!$since){
            return false;
        }
        return $since;
    }

    //修改某个商户自提点
    public static function since_edit($data, $where){
        $since = SinceModel::update($data, $where);
        if(!$since){
            return false;
        }
        return $since;
    }

    //删除商户自提点
    public static function since_del($where){
        $since = SinceModel::update([
            'since_is_delete' => '1'
        ],$where);
        if(!$since){
            return false;
        }
        return true;
    }


    // 运营后台自提点列表
    public static function showlist($pagesize) 
    {
        $where['since_is_delete'] = ['eq', 0];

        $keyword = input('param.keyword');
        $where['a.since_tel|a.since_name'] = ['like', "%$keyword%"];
 
        $data = SinceModel::alias('a')
                -> field('a.since_id,a.since_name,a.since_business_hours,a.since_detailed_address,a.since_tel,c.store_id,c.store_name')
                -> join('merchant b', 'a.since_merchant_id=b.merchant_id', 'left')
                -> join('store c', 'b.merchant_id=c.store_merchant_id', 'left')
                -> where($where)
                -> order('a.since_id', 'desc')
                -> paginate($pagesize);


        return $data;
    } 
   

    // 运营后台查看自提点
    public static function getSince($sinceId)
    {
        $data = SinceModel::find($sinceId);
        return $data;
    } 

    // 运营后台删除自提点
    public static function changeDel($id)
    {
        $result = SinceModel::where('since_id', $id)->setField('since_is_delete', 1);
        if(!$result)
            return false;

        return true;
    }  
}